/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.lucene.face;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wltea.analyzer.IKSegmentation;
import org.wltea.analyzer.Lexeme;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 分词服务
 */
public class WordAnalyzerFace {
    private static final Logger log = LoggerFactory.getLogger(WordAnalyzerFace.class);

    /**
     * 将文件内容分词
     *
     * @param file
     * @return List<Object[0：词元，1：词频]>
     */
    public static List<Object[]> parseWordCaseForSortList(File file) {
        return turnSortList(parseWordCase(file));
    }

    /**
     * 将html文件内容分词
     *
     * @param file
     * @return List<Object[0：词元，1：词频]>
     */
    public static List<Object[]> parseHtmlWordCaseForSortList(String Html) {
        return turnSortList(parseHtmlWordCase(Html));
    }

    /**
     * 将文件内容分词
     *
     * @param file 文本文件
     * @return Map<String词元, Integer词频>
     */
    public static Map<String, Integer> parseWordCase(File file) {
        IKSegmentation iks = null;
        Map<String, Integer> map = new HashMap<String, Integer>();
        try {
            iks = new IKSegmentation(new InputStreamReader(new FileInputStream(
                    file)), false);
            while (true) {
                Lexeme lex = iks.next();
                if (lex == null) {
                    break;
                } else {
                    Integer num = map.get(lex.getLexemeText());
                    // System.out.println(lex.getLexemeText()+"-------"+lex.getLexemeType());
                    if (lex.getLexemeType() >= 4
                            && lex.getLexemeText().length() < 2) {
                        continue;
                    }
                    if (lex.getLexemeType() < 4
                            && lex.getLexemeText().length() < 6) {
                        continue;
                    }
                    if (num == null) {
                        map.put(lex.getLexemeText(), 1);
                    } else {
                        map.put(lex.getLexemeText(), map.get(lex
                                .getLexemeText()) + 1);
                    }
                }
            }

        } catch (IOException e) {
            log.error(e.getMessage());
        }
        return map;
    }

    /**
     * 将文件内容分词
     *
     * @param file 文本文件
     * @return Map<String词元, Integer词频>
     */
    public static Map<String, Integer> parseHtmlWordCase(String html) {
        IKSegmentation iks = null;
        Map<String, Integer> map = new HashMap<String, Integer>();
        try {
            iks = new IKSegmentation(new InputStreamReader(
                    new ByteArrayInputStream(html.getBytes("UTF-8")), "UTF-8"), false);
            while (true) {
                Lexeme lex = iks.next();
                if (lex == null) {
                    break;
                } else {
                    Integer num = map.get(lex.getLexemeText());
                    // System.out.println(lex.getLexemeText()+"-------"+lex.getLexemeType());
                    if (lex.getLexemeType() >= 4
                            && lex.getLexemeText().length() < 2) {
                        continue;
                    }
                    if (lex.getLexemeType() < 4
                            && lex.getLexemeText().length() < 6) {
                        continue;
                    }
                    if (num == null) {
                        map.put(lex.getLexemeText(), 1);
                    } else {
                        map.put(lex.getLexemeText(), map.get(lex
                                .getLexemeText()) + 1);
                    }
                }
            }

        } catch (IOException e) {
            log.error(e.getMessage());
        }
        return map;
    }

    private static List<Object[]> turnSortList(Map<String, Integer> WordCaseMap) {
        Map<String, Integer> map = WordCaseMap;
        List<Object[]> list = new ArrayList<>();
        for (String key : map.keySet()) {
            Object[] obj = {key, map.get(key)};
            list.add(obj);
        }
        Collections.sort(list, (o1, o2) -> {
            Object[] obj1 = o1;
            Object[] obj2 = o2;
            Integer int1 = (Integer) obj1[1];
            Integer int2 = (Integer) obj2[1];
            return -int1.compareTo(int2);
        });
        return list;
    }
}
